﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Smith.ImageConverter
{
    public interface IResize
    {
        Bitmap Resize(Bitmap image);
    }

    public class ImageResizer : IResize
    {
        public virtual Bitmap Resize(Bitmap image)
        {
            return new Bitmap(image);
        }
    }

    public class WiseImageResizer : IResize
    {
        public WiseImageResizer(int longSideLen)
        {
            LongSideLength = longSideLen;
        }

        public Bitmap Resize(Bitmap image)
        {
            return new Bitmap(image, GetNewSize(image.Width, image.Height));
        }

        private Size GetNewSize(int width, int height)
        {
            if (width >= height)
                return new Size(
                    LongSideLength, 
                    Convert.ToInt32((float)height * (float)LongSideLength / (float)width));
            else
                return new Size(
                    Convert.ToInt32((float)width * (float)LongSideLength / (float)height), 
                    LongSideLength);
        }

        public int LongSideLength { get; private set; }
    }

    public class WidthImageResizer : IResize
    {
        public WidthImageResizer(int width)
        {
            NewWidth = width;
        }

        public Bitmap Resize(Bitmap image)
        {
            int w = image.Width;
            int h = image.Height;
            return new Bitmap(image, GetNewSize(w, h));
        }

        private Size GetNewSize(int width, int height)
        {
            return new Size(NewWidth, Convert.ToInt32((float)height * (float)NewWidth / (float)width));
        }

        public int NewWidth { get; private set; }
    }

    public class HeightImageResizer : IResize
    {
        public HeightImageResizer(int height)
        {
            NewHeight = height;
        }

        public Bitmap Resize(Bitmap image)
        {
            int w = image.Width;
            int h = image.Height;
            return new Bitmap(image, GetNewSize(w, h));
        }

        private Size GetNewSize(int width, int height)
        {
            return new Size(Convert.ToInt32((float)width * (float)NewHeight / (float)height), NewHeight);
        }

        public int NewHeight { get; private set; }
    }

    public class CustomizedResizer : IResize
    {
        public CustomizedResizer(int width, int height)
        {
            NewWidth = width;
            NewHeight = height;
        }

        public Bitmap Resize(Bitmap image)
        {
            return new Bitmap(image, new Size(NewWidth, NewHeight));
        }

        public int NewWidth { get; private set; }

        public int NewHeight { get; private set; }
    }

    public class PercentageResizer : IResize
    {
        public PercentageResizer(double perc)
        {
            Percentage = perc;
        }

        public Bitmap Resize(Bitmap image)
        {
            return new Bitmap(image, GetNewSize(image.Width, image.Height));
        }

        private Size GetNewSize(int width, int height)
        {
            return new Size(Convert.ToInt32(width * Percentage), Convert.ToInt32(height * Percentage));
        }

        public double Percentage { get; private set; }
    }
}
